/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ManagedBeans;

import Beans.Company;
import java.awt.Window;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.primefaces.event.FileUploadEvent;
import org.primefaces.event.FlowEvent;
import org.primefaces.model.UploadedFile;
import radio.entities.Accounts;
import radio.entities.AccountsType;
import radio.entities.PaymentDetails;
import radio.entities.PaymentDetailsPK;
import radio.entities.PaymentRate;
import radio.sessions.AccountsFacade;
import radio.sessions.AccountsTypeFacade;
import radio.sessions.CompanyFacade;
import radio.sessions.PaymentDetailsFacade;
import radio.sessions.PaymentRateFacade;

/**
 *
 * @author Kim
 */
public class RegisCompanyWizard {

    @EJB
    private PaymentDetailsFacade paymentDetailsFacade;
    @EJB
    private PaymentRateFacade paymentRateFacade;
    @EJB
    private AccountsTypeFacade accountsTypeFacade;
    @EJB
    private CompanyFacade companyFacade;
    @EJB
    private AccountsFacade accountsFacade;
    /** Creates a new instance of RegisCompanyWizard */
    private Company com = new Company();
    private String plan;
    private boolean skip;
    private static Log logger = LogFactory.getLog(RegisCompanyWizard.class);
    private UploadedFile file;

    public String getPlan() {
        return plan;
    }

    public void setPlan(String plan) {
        this.plan = plan;
    }

    public Company getCom() {
        return com;
    }

    public void setCom(Company com) {
        this.com = com;
    }

    public RegisCompanyWizard() {
    }

    public UploadedFile getFile() {
        return file;
    }

    public void setFile(UploadedFile file) {
        this.file = file;
    }

    public boolean isSkip() {
        return skip;
    }

    public void setSkip(boolean skip) {
        this.skip = skip;
    }

    public void upload() {
        FacesMessage msg = new FacesMessage("Succesful", file.getFileName() + " is uploaded.");
        FacesContext.getCurrentInstance().addMessage(null, msg);
    }

    public void handleFileUpload(FileUploadEvent event) {
//        if (event.getFile().getSize() > (1024 * 1024 * 2)) {
//            FacesMessage msg = new FacesMessage("File size must greater 2MB");
//            FacesContext.getCurrentInstance().addMessage(null, msg);
//            System.out.println(Window.getWindows());
//            
//        } else {
        logger.info("Uploaded: {}" + event.getFile().getFileName());
        file = event.getFile();
        System.out.println(file.getFileName() + "asdfasdfas");
        FacesMessage msg = new FacesMessage("Succesful", event.getFile().getFileName() + " is uploaded.");
        FacesContext.getCurrentInstance().addMessage(null, msg);
        com.setImg("./../images/user/" + event.getFile().getFileName());
        System.out.println(com.getImg());
//        }
    }

    public void save() {
        try {
            HttpSession ses = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
            String userid = String.valueOf(ses.getAttribute("userId"));
            Accounts acc = accountsFacade.find(Integer.parseInt(userid));

            AccountsType type = accountsTypeFacade.getId("Basic Company");
            acc.setTypeId(type);

            PaymentRate pay = null;
            System.out.println(this.plan);
            if (plan.equalsIgnoreCase("month")) {
                pay = paymentRateFacade.findid("Registration", 30);
            } else {
                pay = paymentRateFacade.findid("Registration", 90);
            }
            PaymentDetailsPK pk = new PaymentDetailsPK(acc.getAccId(), pay.getPaymentId(), new java.util.Date());
            PaymentDetails paymentdetail = new PaymentDetails(pk, pay.getPaymentPrice(), false);
            paymentDetailsFacade.create(paymentdetail);
            radio.entities.Company company = new radio.entities.Company(acc.getAccId(), com.getCompanyname(), com.getDesignation(), com.getFax());
            companyFacade.create(company);
            FacesMessage msg = new FacesMessage("Successful", "You has been registed to basic company");
            FacesContext.getCurrentInstance().addMessage(null, msg);
        } catch (Exception e) {
            FacesMessage msg = new FacesMessage("Upgrade Fail");
            e.printStackTrace();
            FacesContext.getCurrentInstance().addMessage(null, msg);
        }
    }

    public String onFlowProcess(FlowEvent event) {
        System.out.println(Window.getOwnerlessWindows());
        logger.info("Current wizard step:" + event.getOldStep());
        logger.info("Next step:" + event.getNewStep());
        if (skip) {
            skip = false;
            return "confirm";
        } else {
            return event.getNewStep();
        }
    }
}
